package com.swk.path;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 各种路径测试
 */
@WebServlet("/PathTest")
public class PathTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public PathTest() {
        super();
    }

	/**
	 * test
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String uri = request.getRequestURI();//uri-->/webDetail/PathTest
		String url = request.getRequestURL().toString();//url-->http://localhost:8080/webDetail/PathTest
		System.out.println("uri-->" + uri);
		System.out.println("url-->" + url);
		
		//绝对路径
		File file1 = new File("D:/eclipse/aa.txt");
		if(file1.exists()){
			System.out.println("file1存在。。。");
		}
		
		//相对路径，"aa.txt" == System.getProperty("user.dir") + "/aa.txt"
		File file2 = new File("aa.txt");
		if(file2.exists()){
			System.out.println("file2存在。。。");
		}
		
		//在eclipse内启动服务器：user.dir-->D:\eclipse
		//在服务器安装目录bin目录下用startup启动程序启动服务器：user.dir-->D:\apache-tomcat-7.0.77\bin
		String path = System.getProperty("user.dir");
		System.out.println("user.dir-->" + path);
		
		File file3 = new File(System.getProperty("user.dir") + "/aa.txt");
		if(file3.exists()){
			System.out.println("file3存在。。。");
		}
		
		System.out.println("------------------------------------------------");
		
		/*
		 * Class.getResource()有2种方式，绝对路径和相对路径。
		 * 绝对路径以/开头，从classpath或jar包根目录下开始搜索；相对路径是相对当前class所在的目录，允许使用..或.来定位文件。
		 * 
		 * ClassLoader.getResource()只能使用绝对路径，而且不能以/开头。
		 */
		
		//file:/D:/apache-tomcat-7.0.77/webapps/webDetail/WEB-INF/classes/com/swk/path/
		System.out.println("path1-->" + PathTest.class.getResource("./"));
		
		//file:/D:/apache-tomcat-7.0.77/webapps/webDetail/WEB-INF/classes/
		System.out.println("path2-->" + PathTest.class.getResource("/"));
		
		System.out.println("------------------------------------------------");
		
		//file:/D:/apache-tomcat-7.0.77/webapps/webDetail/WEB-INF/classes/
		System.out.println("path3-->" + PathTest.class.getClassLoader().getResource(""));
		
		//file:/D:/apache-tomcat-7.0.77/webapps/webDetail/WEB-INF/classes/
		System.out.println("path4-->" + Thread.currentThread().getContextClassLoader().getResource(""));
		
		System.out.println("------------------------------------------------");
		
		//--/webDetail
		System.out.println(request.getContextPath());
		
		//--D:\apache-tomcat-7.0.77\webapps\webDetail\
		System.out.println(request.getServletContext().getRealPath("/"));
		
//		File file = new File(request.getContextPath() + "/config3.properties");
		File file = new File(request.getServletContext().getRealPath("/") + "config3.properties");
		if(file.exists())
			System.out.println("ok...");
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
